package com.soft.springhrms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.soft.springhrms.mapper.RolesMapper;
import com.soft.springhrms.pojo.Users;
import com.soft.springhrms.service.UsersService;
import com.soft.springhrms.mapper.UsersMapper;
import com.soft.springhrms.utils.MD5Util;
import com.soft.springhrms.vo.ResultVO;
import com.soft.springhrms.vo.UsersVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DELL
 * @description 针对表【users】的数据库操作Service实现
 * @createDate 2024-11-16 18:48:18
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private RolesMapper rolesMapper;

    @Override
    public ResultVO addUser(Users users) {
        // 查询手机号是否已注册
        QueryWrapper<Users> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone_number", users.getPhoneNumber());
        if (userMapper.selectOne(queryWrapper) != null) {
            throw new RuntimeException("手机号已被注册！");
        }

        // 对密码进行MD5加密
        String encryptedPassword = MD5Util.encrypt(users.getEncryptedPassword());
        users.setEncryptedPassword(encryptedPassword);  // 存储加密后的密码

        // 设置其他默认属性
        users.setCreatedAt(new Date());
        users.setRoleId(4);  // 默认角色为“未认证用户”
        userMapper.insert(users);

        return ResultVO.success("注册成功");
    }


    @Override
    public UsersVO findByPhoneNumber(String phoneNumber, String encryptedPassword) {
        return userMapper.findByPhoneNumber(phoneNumber, encryptedPassword);
    }

    @Override
    public ResultVO getUserEcharts() {
        Map<String,Object> map =new HashMap<>();
        List<String> chartTypelist = rolesMapper.namelist();
        List<Integer> chartDatalist =userMapper.roleIdlist();
        map.put("chartType",chartTypelist);
        map.put("chartData",chartDatalist);
        return ResultVO.success(map,"获取用户统计成功");
    }


}




